package com.rm.zeyou.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.entity.TechnicianBack;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.TechnicianBackMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.TechnicianBackService;
import com.rm.zeyou.util.back.BankCardUtils;
import com.rm.zeyou.util.back.GetColor;
import com.rm.zeyou.util.back.vo.GetCardDetailVo;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.AddTechnicianBackVo;
import com.rm.zeyou.vo.response.SelectTechnicianBackResponseVo;
import com.rm.zeyou.vo.response.TechnicianBackResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 技师银行卡 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:09
 */
@Service
public class TechnicianBackServiceImpl extends ServiceImpl<TechnicianBackMapper, TechnicianBack> implements TechnicianBackService {
    @Resource
    private TechnicianBackMapper technicianBackMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> addTechnicianBack(Long tid, AddTechnicianBackVo addVo) {
        BankCardUtils.checkBankCard(addVo.getBackldCard());
        //查询银行卡有没有绑定
        Integer isBindBack = technicianBackMapper.selectIsBindBack(addVo.getBackldCard());
        if (isBindBack > 0) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.BACK_IS_EXIST);
        }
        GetCardDetailVo cardDetail = BankCardUtils.getCardDetail(addVo.getBackldCard());

        String typeOfBank = BankCardUtils.getTypeOfBank(cardDetail.getCardType());
        TechnicianBack technicianBack = new TechnicianBack();
        technicianBack.setTid(tid);
        technicianBack.setBackIdcard(addVo.getBackldCard());
        technicianBack.setBackBranch(addVo.getBackBranch());
        technicianBack.setBackUserName(addVo.getBackUserName());
        technicianBack.setBackName(addVo.getBackName() + typeOfBank);
        technicianBack.setBackLogo(cardDetail.getBankImg());
        technicianBack.setBackColor(GetColor.getSexColor(cardDetail.getBankImg()));
        int insert = technicianBackMapper.insert(technicianBack);
        return ApiRestResponse.success(insert);
    }

    @Override
    public ApiRestResponse<SelectTechnicianBackResponseVo> selectTechnicianBackList(Long tid) {
        SelectTechnicianBackResponseVo vo = new SelectTechnicianBackResponseVo();
        List<TechnicianBackResponseVo> technicianBacks = technicianBackMapper.selectTechnicianBackList(tid);
        if (!CheckEmptyUtil.isEmpty(technicianBacks)) {
            technicianBacks.forEach(item -> {
                item.setBackIdcard(DesensitizedUtil.bankCard(item.getBackIdcard()));
            });
            vo.setBackList(technicianBacks);
            vo.setBackCount(technicianBacks.size());
            return ApiRestResponse.success(vo);
        }
        vo.setBackList(technicianBacks);
        vo.setBackCount(technicianBacks.size());
        return ApiRestResponse.success(vo);
    }
}
